//Void ScanLineRGB24ToRGB24WithOpacity(
//                Pixel* dst, Pixel* src, Int32 stride, UInt8 srcOpacity)
//;R0 = dst, R1 = src, R2 = stride, R3 = srcOpacity
// *dst = (srcOpacity * (*src - *dst) + 256 * (*dst)) >> 8

//    pDst        RN 0
//    pSrc        RN 1
//    width       RN 2
//    alpha       RN 3
//
//    xx          RN 4
//    yy          RN 5
//    x           RN 6
//    y           RN 7
//    z           RN 8
//
//    mask        RN 14

#include <asm.h>

.global C_SYMBOL(ScanLineRGB24ToARGB32WithOpacity)
C_SYMBOL(ScanLineRGB24ToARGB32WithOpacity):

    STMFD   SP!, {R4-R11, LR}

    LDR     R4, [R1], #4                // [ x3, x2, x1, x0 ]
    LDR     R5, [R0]                    // [ y3, y2, y1, y0 ]
	MOV	    LR, #16711680
	ADD	    LR, LR, #255                //lr: 00FF00FF

ScanLineRGB24ToARGB32WithOpacity_Loop:

    ORR     R4, R4, #0xFF000000         // [ 255, x2, x1, x0 ]

    AND     R6, LR, R4                  //R6 [ 0, S2, 0, S0 ]
    AND     R7, LR, R4, LSR #8          //R7 [ 0, S3, 0, S1 ]

    AND     R8, LR, R5                  //R8 [ 0, d2, 0, d0 ]
    AND     R9, LR, R5, LSR #8          //R9 [ 0, d3, 0, d1 ]

    SUB     R6, R6, R8                  // [ 0, x2 - y2, 0, x0 - y0 ]
    MUL     R10, R6, R3                 // [ a * (x2 - y2), a * (x0 - y0) ]

    SUBS    R2, R2, #1
    LDRNE   R4, [R1], #4                // [ x3, x2, x1, x0 ]
    LDRNE   R5, [R0, #4]                // [ y3, y2, y1, y0 ]

    SUB     R7, R7, R9                  // [ 0, (x3 - y3), 0, x1 - y1 ]
    MUL     R11, R7, R3                 // [ a * (x3 - y3), a * (x1 - y1) ]

    ADD     R10, R10, R8, LSL #8
    ADD     R11, R11, R9, LSL #8

    AND     R10, LR, R10, LSR #8
    AND     R11, LR, R11, LSR #8

    ORR     R11, R10, R11, LSL #8       // [ 0, (a * (x2 - y2) + (y2<<8)) >> 8, (a * (x1 - y1) + (y1<<8)) >> 8, (a * (x0 - y0) + (y0<<8)) >> 8 ]
    STR     R11, [R0], #4

	BNE 	ScanLineRGB24ToARGB32WithOpacity_Loop

ScanLineRGB24ToARGB32WithOpacity_End:
    LDMFD   SP!, {R4-R11, PC}



